﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

namespace DAL
{
    /// <summary>
    /// DAL to LeaseType table
    /// </summary>
    public abstract class LeaseTypeDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Contansts
        #endregion

        #region Objects
        #endregion

        #region Properties
        #endregion

        #region Constructors
        public LeaseTypeDAL() { }
        public LeaseTypeDAL(string connectString) : base(connectString) { }
        #endregion

        #region Implements
        /// <summary>
        /// Count all of records
        /// </summary>
        /// <returns>Number of records</returns>
        public int Count()
        {
            return _mdb.tb_LeaseTypes.Count();
        }

        /// <summary>
        /// Select all data from table in database
        /// </summary>
        /// <returns>Data</returns>
        public DataTable Select()
        {
            try
            {
                var res = from s in _mdb.tb_LeaseTypes
                          orderby s.Name
                          select new
                          {
                              s.Id,
                              s.Name
                          };
                return res.ToDataTable();
            }
            catch { return _dtb; }
        }

        /// <summary>
        /// Select data by object from table in database
        /// </summary>
        /// <param name="obj">Condition</param>
        /// <returns>Data</returns>
        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Get a object by key
        /// </summary>
        /// <param name="key">Key need to get</param>
        /// <returns>Object</returns>
        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Delete data by id
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns>True is successfull else false</returns>
        public UTL.Session.PerState Delete(long id)
        {
            try
            {
                _mdb = new xG14DataContext();

                var res = from s in _mdb.tb_LeaseTypes
                          where s.Id == id
                          select s;

                if (res != null)
                {
                    var et = res.Single();
                    _mdb.tb_LeaseTypes.DeleteOnSubmit(et);
                    _mdb.SubmitChanges();

                    return UTL.Session.PerState.Success;
                }
                else { return UTL.Session.PerState.Undelete; }
            }
            catch { return UTL.Session.PerState.Undelete; }
        }

        /// <summary>
        /// Insert object to table in database
        /// </summary>
        /// <param name="obj">Object need to insert</param>
        /// <returns>True is successfull else false</returns>
        public UTL.Session.PerState Insert(object obj)
        {
            var o = (tb_LeaseType)obj;

            if (o.Name + "" == "") return UTL.Session.PerState.Empty;

            try
            {
                _mdb.tb_LeaseTypes.Single(k => k.Name == o.Name);

                return UTL.Session.PerState.Duplicate;
            }
            catch
            {
                try
                {
                    _mdb.tb_LeaseTypes.InsertOnSubmit(o);
                    _mdb.SubmitChanges();

                    return UTL.Session.PerState.Success;
                }
                catch { return UTL.Session.PerState.Uninsert; }
            }
        }

        /// <summary>
        /// Update object to table in database
        /// </summary>
        /// <param name="obj">Object need to update</param>
        /// <returns>True is successfull else false</returns>
        public UTL.Session.PerState Update(object obj)
        {
            var o = (tb_LeaseType)obj;

            if (o.Name + "" == "") return UTL.Session.PerState.Empty;

            try
            {
                var res = _mdb.tb_LeaseTypes.Single(k => k.Id == o.Id || k.Name == o.Name);
                res.Name = o.Name;

                _mdb.SubmitChanges();

                return UTL.Session.PerState.Success;
            }
            catch { return UTL.Session.PerState.Unupdate; }
        }
        #endregion

        #region Events
        #endregion

        #region Methods
        #endregion

        #region Overrides
        #endregion

        #region Statics
        #endregion

        #region More
        #endregion
    }
}